<?php
require_once dirname(__FILE__) . '/class.AbstractEDOHandler.php';

class HandicappedEDOHandler extends AbstractEDOHandler
{
	
	public function isTransactional()
	{
		return true;
	}
		
	
	public function insert(EDO_object_base $obj, $clear = true)
	{
		$typeid = $obj->getTypeId();  
	    $edoid = $obj->getField('edoid');
	    
		if($edoid === false OR is_null($edoid) OR $clear)
		{
			$edoid = $obj->ldb->nextId('edoid');
			$obj->setField('edoid', $edoid);
		}
		else
		{
			$edoid = $obj->getField('edoid');
		}

    	if($dasField = $obj->getFieldObject('automatic_created_on'))
		{
		    $dasField->setValue(date('Y-m-d H:i:s', time()));
        	if($dasField = $obj->getFieldObject('automatic_modified_on'))
    		{
	       	    $dasField->setValue(date('Y-m-d H:i:s', time()));
		    }
		}

		$errorfree = array();

		$phrase = "INSERT INTO edo_objects (edoid, typeid) VALUES ({$edoid}, {$typeid})";

		$errorfree[] = $obj->ldb->exec($phrase);
		foreach($obj->getTables() as $tablekey => $tablename)
		{
			$darray = array();
			foreach($obj->getTablesFields($tablekey) as $field)
			{
				//if(!$clear OR !is_null($obj->getField($field['name'])))
				$darray[$field] = $obj->getFieldForSQL($field);


			}
			
			
		    
            if($obj->tableIsDimensional($tablename))
                $darray['dimensionid'] = call_user_func($this->getHandler()->getAttribute('Callback_GetDimension'));
            $errorfree[] = $obj->ldb->autoExecute($tablename, $darray, LDB::AUTOQUERY_INSERT);
            			
		}
		$errorfree = (!in_array(false, $errorfree, true));
				
		// $errorfree ? $obj->ldb->commit() : $obj->ldb->rollBack();
		
		if($errorfree)
		{
			$obj->triggerEvent('onInsertSuccess');
			return $edoid;
		}
		else 
		{
			$obj->triggerEvent('onInsertFailed');
			return false;
		}
		

		
	}
	
	
	public function save(EDO_object_base $obj)
	{
		// $obj->ldb->beginTransaction();
		$errorfree = array();
		
    	if($dasField = $obj->getFieldObject('automatic_modified_on'))
		{
		    $dasField->setValue(date('Y-m-d H:i:s', time()));
		}
		
		foreach($obj->getTables() as $tablekey => $tablename)
		{
			$darray = array();
			foreach($obj->getTablesFields($tablekey) as $field)
			{
				$darray[$field] = $obj->getFieldForSQL($field);
			}
		  
            if(!$obj->tableIsDimensional($tablename))
			     $errorfree[] = $obj->ldb->autoExecute($tablename, $darray, LDB::AUTOQUERY_UPDATE, "edoid = {$obj->getField('edoid')}");
			else
			     $errorfree[] = $obj->ldb->autoExecute($tablename, $darray, LDB::AUTOQUERY_UPDATE, "edoid = {$obj->getField('edoid')} AND dimensionid = {$obj->getField('dimensionid')}");
		}
		
		$errorfree = (!in_array(false, $errorfree, true));
		// $errorfree ? $obj->ldb->commit() : $obj->ldb->rollBack();

		if($errorfree)
		{
			$obj->triggerEvent('onSaveSuccess');
			return true;
		}

		$obj->triggerEvent('onSaveFailed');
		return false;


		
		
	}
	
	

	
	private function _delete(EDO_object_basic $obj)
	{
	  	$tables = $obj->getTables();

	  	$phrases = array();
	  	
	  	foreach($tables as $table)	  	
	  	{
	  	    $phrases[] = "DELETE FROM {$table} WHERE edoid = {$obj->getField('edoid')}";
	  	    
	  	}
	  	
	  	foreach($obj->getRelationObjects() as $relObj)
	  	{
	  	    if(is_a($relObj, 'Datatype_relation_has_and_belongs_to_many'))
	  	    {
	  	        $phrases[] = "DELETE FROM {$relObj->getJoinTable()} WHERE {$obj->getClass()}_edoid = {$obj->edoid}";
	  	    }
	  	}
	  	
		$errorfree = array();
		
		foreach($phrases as $phrase)
		{
            $errorfree[] = $obj->ldb->exec($phrase);    
		}
		
		$errorfree = (in_array(false, $errorfree, true)) ? false : true;
		
		// $errorfree ? $obj->ldb->commit() : $obj->ldb->rollBack();
		return $errorfree ? true : false;
	}
	
	
	
	
	
	public function delete($idRes)
	{
	   if($idRes instanceof EDO_object_base)
	   {
	       return $this->_delete($idRes);
	   }
	   else 
	   {
	       throw new Exception('Invalid identifier resource');
	   }
	}
	
	
	
}